<?php

namespace App\Console\Commands;

use App\Common\Constant\Params;
use App\Models\Master;
use App\Models\MasterDetail;
use App\Models\MasterStatistic;
use App\Models\MasterWallet;
use App\Models\Merchant;
use App\Models\MerchantOrderNum;
use App\Models\OldMaster;
use App\Models\OldMerchant;
use App\Models\Province;
use App\Services\IAreaService;
use App\Services\IMasterService;
use App\Services\IStorageService;
use DB;
use Illuminate\Console\Command;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Collection;
use Log;

class importMerchant extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'import:old-merchant {offset=0}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '导入商家';

    private $exceptionHandler;
    private $masterService;
    private $storageService;
    private $areaService;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(ExceptionHandler $exceptionHandler,IMasterService $masterService,IStorageService $storageService,IAreaService $areaService)
    {
        parent::__construct();

        $this->exceptionHandler = $exceptionHandler;
        $this->masterService = $masterService;
        $this->storageService = $storageService;
        $this->areaService = $areaService;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $bar = $this->output->createProgressBar(OldMerchant::count());
        $offset = $this->argument('offset');

        $areas = getAreas();
        $area_names = [];
        foreach ($areas as $area){
            $area_names[$area['n']] = $area;
        }

        OldMerchant::orderBy('me_id')->chunk(100, function ($merchants) use (&$bar,&$area_names,&$offset){
            foreach ($merchants as $model) {
                if ($bar->getProgress() < $offset){
                    $this->line("\tMerchant [{$model->me_id}] skip");
                    $bar->advance();
                    continue;
                }

                DB::beginTransaction();
                try{
                    $ret = Merchant::create($model->toArray()+['me_weixin'=>'','me_bank_name'=>''])
                    && MerchantOrderNum::create([
                        'me_id'=>$model->me_id,
                        'order_type' => 1,
                    ]) && MerchantOrderNum::create([
                        'me_id'=>$model->me_id,
                        'order_type' => 2,
                    ]);
                    if (!$ret){
                        throw new \Exception('Merchant order num create fail');
                    }

                    DB::commit();
                    $this->info("\tMerchant [{$model->me_id}] success");
                }catch (\Exception $e){
                    DB::rollBack();
                    $this->error("\tMerchant [{$model->me_id}] fail\nerror: ".$e->getMessage()." in ".$e->getFile()."[".$e->getLine()."]\n");
                    Log::error("\tMerchant [{$model->me_id}] fail\nerror: ".$e->getMessage()." in ".$e->getFile()."[".$e->getLine()."]\n");
                    $this->exceptionHandler->report($e);
                }

                $bar->advance();
            }
        });

        $bar->finish();

    }
}
